package net.janeluo.rs.module;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.janeluo.rs.entity.RS_Orders;
import net.janeluo.rs.entity.RS_User;
import net.janeluo.rs.service.ChangerPwdService;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

/**
 * 用户密码修改
 * 
 * @author DLK)JaneLuo
 * @version 1.0.0
 */
@IocBean
@At("/")
public class ChangePwdModule {
    
    @Inject
    private ChangerPwdService changerPwdService;
    
    /**
     * 初始化修改密码
     * @return 
     */
    @At("InitChangePwd")
    @Ok("jsp:views.ChangePwd")
    public List<RS_Orders> myOrder(HttpServletRequest req, @Param("type") String type, @Param("flag") String flag){
        RS_User user = (RS_User) req.getSession().getAttribute("user");
        req.getSession().setAttribute("menuName", "UpdatePassword");
        req.setAttribute("flag", flag);
        return null;
    }
    
    /**
     * 校验密码是否正确
     * 
     * @param req
     * @param pwd
     * @return
     */
    @At("CheckUserPwd")
    @Ok("json")
    public Map<String, String> checkUserPwd(HttpServletRequest req, @Param("userpwd") String pwd){
        RS_User user = (RS_User) req.getSession().getAttribute("user");
        String res = changerPwdService.checkUserPwd(user.getUserid(), pwd);
        Map<String, String> resMap = new HashMap<String, String>();
        resMap.put("getdata", res);
        return resMap;
    }
    
    /**
     * 更新密码
     * 
     * @param req
     * @param pwd
     */
    @At("UpdateUserPwd")
    @Ok("->:/InitChangePwd.nut?flag=1")
    public void updateUserPwd(HttpServletRequest req, @Param("newPassWord") String pwd){
        RS_User user = (RS_User) req.getSession().getAttribute("user");
        user.setPassWord(pwd);
        changerPwdService.updateUserPwd(user);
    }

}
